/*
 * RED5 HLS plugin - https://github.com/mondain/red5-hls-plugin
 * 
 * Copyright 2006-2013 by respective authors (see below). All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.red5.xuggler.tool;

import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

import org.red5.service.httpstream.SegmentFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xuggle.mediatool.MediaToolAdapter;
import com.xuggle.mediatool.event.ICloseEvent;
import com.xuggle.mediatool.event.IVideoPictureEvent;
import com.xuggle.xuggler.IPixelFormat.Type;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.IVideoResampler;

/**
 * Video frame dimension adjustment.
 * 
 * @author Paul Gregoire (mondain@gmail.com)
 */
public class VideoAdjustTool implements GenericTool {

	private Logger log = LoggerFactory.getLogger(VideoAdjustTool.class);

	private IVideoResampler resampler = null;

	private int width;

	private int height;

	private Type pixelType = Type.YUV420P;

	private SegmentFacade facade;

	public VideoAdjustTool(int width, int height) {
		log.trace("Video width: {} height: {}", width, height);
		this.width = width;
		this.height = height;
		for (IVideoResampler.Feature feature : IVideoResampler.Feature.values()) {
			if (!IVideoResampler.isSupported(feature)) {
				log.warn("VideoResampler {} feature is not supported", feature);
			}
		}
	}
	long startTime=0;
	long videoTS=0; 
	public void onVideoPicture(BufferedImage bImg) {
		if (startTime == 0) {
			startTime = System.currentTimeMillis();
		}   
		videoTS = 1000 * ( System.currentTimeMillis() - startTime);
		facade.queueVideo(bImg,videoTS, TimeUnit.MILLISECONDS); 
	}

	/* (non-Javadoc)
	 * @see com.xuggle.mediatool.MediaToolAdapter#onClose(com.xuggle.mediatool.event.ICloseEvent)
	 */
	 
	public void onClose(ICloseEvent event) {
		close(); 
	}

	public void close() {
		if (resampler != null) {
			resampler.delete();
		}
	}

	/**
	 * @param facade the facade to set
	 */
	public void setFacade(SegmentFacade facade) {
		this.facade = facade;
	}

}
